package com.bootdo.system.dao;

import com.bootdo.hjh.cache.DefaultNodeInfo;
import com.bootdo.hjh.domain.UserExt;
import com.bootdo.system.domain.UserDO;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.system.vo.ProfileVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2017-10-03 09:45:11
 */
@Mapper
public interface UserDao {

	UserDO get(Long userId);


	List<UserDO> list(Map<String,Object> map);

	int count(Map<String,Object> map);

	int save(UserDO user);

	int update(UserDO user);

	int remove(Long userId);

	int batchRemove(Long[] userIds);

	Long[] listAllDept();

	@Update("update sys_user set name=#{name},card_number=#{cardNumber},live_address=#{liveAddress},sex=#{sex} where user_id=#{userId}")
	int updateProfile(ProfileVO profileVO);

	@Update("update sys_user set pay_pwd=#{payPwd} where user_id=#{userId}")
	Integer updatePayPwd(ProfileVO profileVO);

	@Update("update sys_user set password=#{loginPwd} where user_id=#{userId}")
	Integer updateLoginPwd(ProfileVO profileVO);

	@Update("update sys_user set password=#{loginPwd} where user_id=#{userId}")
	Integer updateLoginPwd1(@Param("loginPwd") String loginPwd,@Param("userId") Long userId);

	@Update("update sys_user set mobile=#{mobile} where user_id = #{userId}")
	Integer updateMobile(@Param("mobile") String mobile, @Param("userId") Long userId);

	@Update("update sys_user set email=#{email} where user_id = #{userId}")
	Integer updateEmail(@Param("email") String email, @Param("userId") Long userId);

	@Select("select username from sys_user where username like #{username} limit 100")
	List<String> searchUserByName(@Param("username") String username);

	@Select("select * from sys_user where username = #{username} limit 1")
	UserDO getByUsername(String username);

	@Select("select * from sys_user where mobile = #{mobile} limit 1")
	UserDO getByMobile(String mobile);

	/**
	 * 获取所有顶级用户ID
	 */
	@Select("select  user_id,level,username,name,mobile,`recommend_user_id` as parent_id, start_user_id as root_id,"
			+ "active_time,member_level,agent_level, stock, points from sys_user where type = 2 and `recommend_user_id` is null")
	List<DefaultNodeInfo> selectRootList();

	/**
	 * 获取顶级用户的后代
	 */
	@Select("select user_id,level,username,name,mobile,`recommend_user_id` as parent_id,start_user_id as root_id,"
			+ "active_time,member_level,agent_level,stock, points from sys_user where type = 2 and start_user_id = "
			+ "#{rootId} and `level`=#{level} order by user_id limit #{start},#{limit}")
	List<DefaultNodeInfo> selectChildrenOfRoot(@Param("rootId") Long rootId,@Param("level") Integer level,@Param("start") int start,@Param("limit") int limit);

	@Select("select user_id,level,username,name,mobile,`recommend_user_id` as parent_id,start_user_id as root_id,"
			+ "active_time,member_level,agent_level, stock, points from sys_user where type = 2 and user_id=#{userId}")
	DefaultNodeInfo getNodeInfoByUserId(Long userId);

	/**
	 * 更新为代理
	 * @param agentLevel 代理级别 1:代理 2:初级合伙人 3:中级合伙人 4:高级合伙人 5:联合创始人
	 * @param userId
	 * @return 	  1q
	 */
	@Update("update sys_user set agent_level=#{agentLevel} where user_id = #{userId}")
	Integer updateAgency(@Param("agentLevel") Integer agentLevel, @Param("userId") Long userId);

	@Select("select * from user_ext where user_id = #{userId}")
	public UserExt getUserExt(Long userId);

	@Select("update user_ext set agent_time = now() where user_id = #{userId}")
	public Integer updateUserExtAgent(Long userId);

	@Select("insert into user_ext(user_id) value(#{userId})")
	public UserExt insertUserExt(Long userId);
	/**
	 * 获取所有VIP用户
	 * @return
	 */
	@Select("select * from sys_user where active_time is not null and active_time >= DATE_SUB(current_timestamp(), INTERVAL 1 YEAR) " +
			" and member_level = 3 ")
	public List<UserDO>getYearVipUser();
	/**
	 * 获取所有代理
	 * @return
	 */
	@Select("select * from sys_user where agent_level = #{level} and type = 2")
	public List<UserDO> getAgentUserList(Integer level);

	/**
	 * 获取所有代理
	 * @return
	 */
	@Select("select * from sys_user where agent_level in (1,2,3,4,5) and type = 2")
	public List<UserDO> getAgen2UserList();
	/**
	 * 初级合伙人
	 * @return
	 */
	@Select("select * from sys_user where agent_level in (2,3,4,5) and type = 2")
	public List<UserDO> getAgent3UserList();
	/**
	 * 中级合伙人
	 * @return
	 */
	@Select("select * from sys_user where agent_level in (3,4,5) and type = 2")
	public List<UserDO> getAgent4UserList();
	/**
	 * 高级合伙人
	 * @return
	 */
	@Select("select * from sys_user where agent_level in (4,5) and type = 2")
	public List<UserDO> getAgent5UserList();

	/**
	 * 获取最新的激活时间
	 * @return
	 */
	@Select("select max(active_time) lastTime from sys_user where type = 2")
	public Date getUserLastActiveTime();

	/**
	 * 需要分红的总金额
	 * @param lastBonusTime 最后一次分红时间
	 * @param currentLastActiveTime 戴上到当前的分红时间
	 * @return
	 */
	@Select("select sum( " +
			"case when member_level = 1 then 1100 when member_level =2 then 3300 when  " +
			" member_level = 3 then 9900 when member_level = 4 then 6600 end ) amount " +
			" from sys_user where " +
			" active_time is not null and  " +
			" active_time > #{lastBonusTime} and active_time <= #{currentLastActiveTime} and type = 2")
	public Integer getBonusAmount(@Param("lastBonusTime") Date lastBonusTime,
			@Param("currentLastActiveTime") Date currentLastActiveTime);

	@Select("select * from sys_user where type = 2 and agent_level = #{agentLevel} and active_time is not null order by user_id desc ")
	public List<UserDO> getAllUser(Integer agentLevel);

	@Select("select * from sys_user where type = 2 and agent_level is null and active_time is not null order by user_id desc ")
	public List<UserDO> getAllUserByAgentIsNull();

	@Select("select * from sys_user where date(active_time) >= date(#{startDate})  "
			+ " and date(active_time) <= date(#{endDate}) and active_time is not null order by active_time desc ")
	public List<UserDO> achievement(@Param("startDate") String startDate,@Param("endDate") String endDate);

	@Update("update sys_user set stock = #{stock} where user_id = #{userId}")
	int updateStock(@Param("stock") int stock, @Param("userId") Long userId);


	@Update("update sys_user set points = #{points} where user_id = #{userId}")
	int updatePoints(@Param("points") int points, @Param("userId") Long userId);

	@Update("update sys_user set agent_level = #{agentLevel} where user_id = #{userId}")
	int updateAgentLevel(@Param("agentLevel") int agentLevel, @Param("userId") Long userId);

	@Select("select user_id, level, recommend_user_id from sys_user")
	List<UserDO> selectAllUserForLevel();

}
